package com.nightsoul.javawebsocket.test;

import java.net.URI;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

public class MyChatClient extends WebSocketClient {
	
	public MyChatClient(URI serverURI) {
		super(serverURI, new Draft_17());
	}

	@Override
	public void onOpen(ServerHandshake handshakedata) {
		System.out.println("client open");
	}

	@Override
	public void onMessage(String message) {
		System.out.println("recv: " + message);		
	}

	@Override
	public void onClose(int code, String reason, boolean remote) {
		System.out.println("has left the room!" );	
	}

	@Override
	public void onError(Exception ex) {
		ex.printStackTrace();		
	}
	
	
	public static void main(String[] args) throws Exception {
		MyChatClient chatClient = new MyChatClient(new URI("ws://localhost:8887"));
		
		// 非阻塞方法，需要对连接状态进行判断
		chatClient.connect();
		
		System.out.println(chatClient.getReadyState());
		
		
		Thread.sleep(5000);
		System.out.println(chatClient.getReadyState());
		
		chatClient.send("Hello");
		
		
	}
}
